<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>关于form表单的一些总结 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="123456789101112131415161718192021222324252627282930313233343536373839404142434445&lt;!DOCTYPE html&gt;&lt;html lang&#x3D;&quot;en&quot;&gt;&lt;head&gt;    &lt;meta charset&#x3D;&quot;UTF-8&quot;&gt;    &lt;titl">
<meta property="og:type" content="article">
<meta property="og:title" content="关于form表单的一些总结">
<meta property="og:url" content="https://gitee.com/demiler/2023/03/02/%E5%85%B3%E4%BA%8Eform%E8%A1%A8%E5%8D%95%E7%9A%84%E4%B8%80%E4%BA%9B%E6%80%BB%E7%BB%93/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="123456789101112131415161718192021222324252627282930313233343536373839404142434445&lt;!DOCTYPE html&gt;&lt;html lang&#x3D;&quot;en&quot;&gt;&lt;head&gt;    &lt;meta charset&#x3D;&quot;UTF-8&quot;&gt;    &lt;titl">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2023-03-01T16:00:00.000Z">
<meta property="article:modified_time" content="2023-03-02T13:42:58.197Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/test1/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/test1/css/style.css">

<meta name="generator" content="Hexo 5.4.2"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/test1/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/test1/">Home</a>
        
          <a class="main-nav-link" href="/test1/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/test1/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://gitee.com/demiler"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-关于form表单的一些总结" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/test1/2023/03/02/%E5%85%B3%E4%BA%8Eform%E8%A1%A8%E5%8D%95%E7%9A%84%E4%B8%80%E4%BA%9B%E6%80%BB%E7%BB%93/" class="article-date">
  <time datetime="2023-03-01T16:00:00.000Z" itemprop="datePublished">2023-03-02</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      关于form表单的一些总结
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Title<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">form</span> <span class="attr">id</span>=<span class="string">&quot;uploadForm&quot;</span> <span class="attr">enctype</span>=<span class="string">&quot;multipart/form-data&quot;</span>&gt;</span></span><br><span class="line">    用户名:<span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">name</span>=<span class="string">&quot;username&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    密码：<span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;password&quot;</span> <span class="attr">name</span>=<span class="string">&quot;password&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    头像:<span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;file&quot;</span> <span class="attr">name</span>=<span class="string">&quot;pic&quot;</span> <span class="attr">id</span>=<span class="string">&quot;pic&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;button&quot;</span> <span class="attr">value</span>=<span class="string">&quot;上传头像&quot;</span> <span class="attr">onclick</span>=<span class="string">&quot;doUpload()&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;js/jquery-3.4.0.min.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">function</span> <span class="title function_">doUpload</span>(<span class="params"></span>)&#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">let</span> formData = <span class="keyword">new</span> <span class="title class_">FormData</span>($(<span class="string">&quot;#uploadForm&quot;</span>)[<span class="number">0</span>]);</span></span><br><span class="line"><span class="language-javascript">        $.<span class="title function_">ajax</span>(&#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="attr">url</span>:<span class="string">&quot;http://localhost:8080/user/reg.do&quot;</span>,</span></span><br><span class="line"><span class="language-javascript">            <span class="attr">type</span>:<span class="string">&quot;post&quot;</span>,</span></span><br><span class="line"><span class="language-javascript">            <span class="attr">data</span>:formData,</span></span><br><span class="line"><span class="language-javascript">            <span class="attr">contentType</span>:<span class="literal">false</span>,<span class="comment">//以表单中enctype=&quot;multipart/form-data&quot;来设置数据格式</span></span></span><br><span class="line"><span class="language-javascript">            <span class="attr">processData</span>:<span class="literal">false</span>,<span class="comment">//对上面设置的格式,不作转换处理</span></span></span><br><span class="line"><span class="language-javascript">            <span class="attr">dataType</span>:<span class="string">&quot;json&quot;</span>,</span></span><br><span class="line"><span class="language-javascript">            <span class="attr">success</span>:<span class="keyword">function</span>(<span class="params">result</span>)&#123;</span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">if</span>(result.<span class="property">code</span>==<span class="number">1</span>)&#123;</span></span><br><span class="line"><span class="language-javascript">                    <span class="title function_">alert</span>(result.<span class="property">msg</span>)</span></span><br><span class="line"><span class="language-javascript">                    location.<span class="property">href</span> = <span class="string">&quot;/login.html&quot;</span>;</span></span><br><span class="line"><span class="language-javascript">                &#125;<span class="keyword">else</span>&#123;</span></span><br><span class="line"><span class="language-javascript">                    <span class="title function_">alert</span>(result.<span class="property">msg</span>)</span></span><br><span class="line"><span class="language-javascript">                &#125;</span></span><br><span class="line"><span class="language-javascript">            &#125;,</span></span><br><span class="line"><span class="language-javascript">            <span class="attr">error</span>:<span class="keyword">function</span>(<span class="params"></span>)&#123;</span></span><br><span class="line"><span class="language-javascript">                <span class="title function_">alert</span>(<span class="string">&quot;error&quot;</span>)</span></span><br><span class="line"><span class="language-javascript">            &#125;</span></span><br><span class="line"><span class="language-javascript">        &#125;)</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>   以上代码块中enctype是规定了form表单在发送到服务器时候的编码格式，默认情况下是application/x-www-form-urlencoded，只能用于传输文本格式的文件。</p>
<p>以下是enctype值的类型总结：</p>
<p>application/x-www-form-urlencoded：</p>
<p>适用对象：所有字符。</p>
<p>格式：表单数据编码为名称/值对。 这是标准编码格式。</p>
<p>其他描述：1.在发送前编码所有字符（默认）。</p>
<p>​                    2.默认的编码方式。但在用文本的传输和MP3等大型文件的时候，使用这种编码就显得效率低下。</p>
<p>multipart/form-data：</p>
<p>适用对象：在使用包含文件上传控件的表单时，必须使用该值。</p>
<p>格式：表单数据编码为消息，页面上每个控件都有单独的部分。</p>
<p>其他描述：1.不对字符编码。</p>
<p>​                   2.指定传输数据为二进制类型，比如图片，mp3，文件。</p>
<p>text/plain： </p>
<p>适用对象：纯文体。</p>
<p>格式：表单数据以纯文本编码，没有任何控件或格式字符。</p>
<p>其他描述：空格转换为 “+” 加号，但不对特殊字符编码。</p>
<p>需注意用ajax传递时，contentType格式应设为false，才能用表单上enctype所规定的编码格式来传递数据格式。</p>
<p>ajax上传文件为什么设置false???</p>
<p>要想了解原因我们需要知道两点:</p>
<p>1.上传过程:先封装了一个 formData 对象，然后使用 post 方法将文件传给服务器，contentType = false</p>
<p>2.http上传文件问题</p>
<p>在 rfc1867 中为http添加了上传功能并限定 form 的 method 必须为 POST,enctype = “multipart/form-data” 。</p>
<p>发现在 multipart/form-data 后面有boundary以及一串字符，这是分界符，后面的一堆字符串是随机生成的，目的是防止上传。</p>
<p>文件中出现分界符导致服务器无法正确识别文件起始位置。multipart/form-data 请求是基于 http 原有的请求方式 post 而来的。</p>
<p>请求头的不同，对于上传文件的请求，contentType = multipart/form-data是必须的</p>
<p>请求体不同，发送的每个字段内容之间必须要使用分界符来隔开</p>
<p>在 ajax 中 contentType 设置为 false 是为了避免 JQuery 对其操作，从而失去分界符，而使服务器不能正常解析文件。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://gitee.com/demiler/2023/03/02/%E5%85%B3%E4%BA%8Eform%E8%A1%A8%E5%8D%95%E7%9A%84%E4%B8%80%E4%BA%9B%E6%80%BB%E7%BB%93/" data-id="clfw134vb0000rov7hd3d5osc" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/test1/2023/03/31/%E5%A4%A9%E8%89%B2%E5%9B%BE%E7%89%87/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          天色图片
        
      </div>
    </a>
  
  
    <a href="/test1/2023/02/28/%E6%88%91%E7%9A%84%E7%AC%AC%E4%B8%80%E7%AF%87%E5%8D%9A%E5%AE%A2/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">我的第一篇博客</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/test1/archives/2023/03/">March 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/test1/archives/2023/02/">February 2023</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/test1/2023/03/31/%E5%A4%A9%E8%89%B2%E5%9B%BE%E7%89%87/">天色图片</a>
          </li>
        
          <li>
            <a href="/test1/2023/03/02/%E5%85%B3%E4%BA%8Eform%E8%A1%A8%E5%8D%95%E7%9A%84%E4%B8%80%E4%BA%9B%E6%80%BB%E7%BB%93/">关于form表单的一些总结</a>
          </li>
        
          <li>
            <a href="/test1/2023/02/28/%E6%88%91%E7%9A%84%E7%AC%AC%E4%B8%80%E7%AF%87%E5%8D%9A%E5%AE%A2/">我的第一篇博客</a>
          </li>
        
          <li>
            <a href="/test1/2023/02/28/hello-world/">Hello World</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2023 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/test1/" class="mobile-nav-link">Home</a>
  
    <a href="/test1/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/test1/fancybox/jquery.fancybox.css">

  
<script src="/test1/fancybox/jquery.fancybox.pack.js"></script>




<script src="/test1/js/script.js"></script>




  </div>
</body>
</html>